#!/usr/bin/python
# coding: utf-8
import sys
import Image

def get_maxspace(list, threhold):
	count = 0
	hi_active = False
	max_count = 0
	
	for v in list:
		if v > threhold:
			if max_count < count:
				max_count = count	
			count = 0
		else:
			count = count + 1
	return max_count	

def split_sum(list, threshold, low_width):
	hi_count = 0
	low_count = 0
	hi_active = False
	count = 0

	for v in list:
		if v > threshold:
			count = 0
			if ( not hi_active ): 
				hi_active = True
				hi_count = hi_count + 1
					
		else:
			count = count + 1
			if ( count <= low_width):
				continue
			if ( hi_active):
				hi_active = False
				low_count = low_count + 1
			
	
	return hi_count

	
	
if __name__ == '__main__':
        argvs = sys.argv
        argc  = len(argvs)
        if ( argc != 2 ):
                print "Usage: tool <FileName>"
                sys.exit(1)
	#ファイルオープン
	f = open(argvs[1])

	list = []
        for line in f:
                list.append(int(line[:-1]))

	#関数をきく
	max_threhold = int(max(list)/8)
	
	#f_list (threhold, splitcount)
	f_list = []
	for idx in range(3,max_threhold):
		f_list.append((idx, split_sum(list, idx, 3)))
	
	#最大のsplitcountでthreholdを決める
	#threhold(first)
	threhold = 0
	max_splitcount = 0
	for idx in range(len(f_list)):
		if max_splitcount < f_list[idx][1]:
			max_splitcount = f_list[idx][1]
			threhold = f_list[idx][0]	

	print "first threhold: ",threhold
	print "max threhold:", int(max(list)/8)
	
	#width(first)
	print "max width:" , get_maxspace(list, threhold)

	#s_list (width, splitcount)
	#計算範囲3 ~ 最大width/2
	s_list = []
	for w in range(3, int(get_maxspace(list, threhold)/2)):
		s_list.append((w, split_sum(list, threhold, w)))
	
	#頻度が高いsplitcountでwidthを決める
	#splitcountが平均以下の値を除く
	mid_splitcount = int(max(map(lambda x: x[1], s_list))/2)
	
	# t_list ( width, splitcount )
	# 中値以上のリスト
	t_list = []
	for w, c in s_list:
		if c >= mid_splitcount :
			t_list.append((w, c))

	#dictionary作成
	dic = {}
	for w, c in t_list:
		if not dic.has_key(c):		
			dic[c] = 1
		else:
			dic[c] = dic[c] + 1
	#最適なwidth
	opt_width, opt_sc = reduce( lambda a,b: a if a[1] > b[1] else b ,dic.items())
		
	print "opt width:",opt_width
	
	#再度処理
	#f_list (threhold, splitcount)
        f_list = []
        for idx in range(3,max_threhold):
                f_list.append((idx, split_sum(list, idx, opt_width)))

        #最大のsplitcountでthreholdを決める
        #threhold(first)
        threhold = 0
        max_splitcount = 0
        for idx in range(len(f_list)):
                if max_splitcount < f_list[idx][1]:
                        max_splitcount = f_list[idx][1]
                        threhold = f_list[idx][0]

        print "first threhold: ",threhold

	f.close()

